package com.hzz.hellomp.test;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class MinIOApp {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://192.168.126.80:9000")
                .credentials("minioadmin","minioadmin")
                .build();

        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("lease-hzz").build());

        if (!found){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("lease-hzz").build());
            String policy ="""
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::hello-minio/*"
                          } ],
                          "Version" : "2012-10-17"
                        }""";

            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket("lease-hzz").config(policy).build());
        }

        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket("lease-hzz")
                        .object("floow.png")
                        .filename("C:\\Users\\10928\\Desktop\\floow.png")
                        .build()
        );

    }
}
